classdef spyfilt_highpass < spyview_filter
   properties (Constant=true)
       name='Highpass';
       description='Highpass filter data by subtracting a smoothed copy.  Sizes are in pixels';
       parameters=struct('name',{'X Size','Y Size'},'type',{'float','float'},'default',{0,0},'min',{0,0},'max',{50,50});
   end
   methods
       function [pm,px,py]=filter(sf,pm,px,py)
           if sf.values{1} == 0 && sf.values{2} == 0
               return;
           end
           xs=ceil(3*sf.values{1})*2+1;
           ys=ceil(3*sf.values{2})*2+1;           
           if sf.values{1} == 0 
               y=((-floor(ys/2)):1:floor(ys/2))';
               kernel=exp(-y.^2/(2*sf.values{2}));
           elseif sf.values{2} == 0
               x=(-floor(xs/2)):1:floor(xs/2);
               kernel=exp(-x.^2/(2*sf.values{1}));
           else
               y=((-floor(ys/2)):1:floor(ys/2));
               x=(-floor(xs/2)):1:floor(xs/2);
               [cx,cy]=meshgrid(x,y);
               kernel=exp(-cx.^2/(2*sf.values{1}) + -cy.^2/(2*sf.values{2}));
           end
           kernel = kernel / sum(kernel(:));
           pmp=padarray(pm,[floor(ys/2),floor(xs/2)],'replicate');
           pmp=conv2(pmp,kernel,'same');
           pmp=pmp(1+floor(ys/2):(end-floor(ys/2)),1+floor(xs/2):(end-floor(xs/2)));
           pm=pm-pmp;
       end
   end
end